/*
   Copyright (c) 2011, 2015 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *  Marco Riccardi 
 *	2011-03-20 - initial implementation
 *  2015-02-07 - added writeFile methods
 *
 */
package org.mizartools.utility;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

public final class UtilityFile {

	private UtilityFile(){}
	
	public static boolean Copy(File sourceFile, File destinationFile) {
		boolean exit = false;
		FileInputStream fileInputStream = null;
		FileOutputStream fileOutputStream = null;
		byte[] buffer = new byte[131071];
		try {
			fileInputStream = new FileInputStream(sourceFile);
		} catch (FileNotFoundException e2) {
			e2.printStackTrace();
			return exit;
		}
		try {
			fileOutputStream = new FileOutputStream(destinationFile);
			int read = fileInputStream.read(buffer);
			while (read != -1){
				fileOutputStream.write(buffer, 0, read);
				fileOutputStream.flush();
				read = fileInputStream.read(buffer);
			}
			fileInputStream.close();
			fileOutputStream.close();
			exit = true;
		} catch (IOException e) {
			e.printStackTrace();
			try {
				fileInputStream.close();
			} catch (IOException e2) {
				e2.printStackTrace();
			}
			if (fileOutputStream != null){
				try {
					fileOutputStream.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}
		return exit;
	}

	public static String readFile(File file) throws IOException{
		String string = "";
		FileReader fileReader = null;
		try {
			fileReader = new FileReader(file);
			int len = (int)file.length();
			char[] cbuf = new char[len];
			fileReader.read(cbuf, 0, len);
			fileReader.close();
			string = String.copyValueOf(cbuf);
		} catch (IOException e) {
			if (fileReader != null) {
				fileReader.close();
			}
		}
		return string;
	}

	public static ArrayList<Byte> readBinaryFile(File file) throws IOException{
		ArrayList<Byte> arrayList = new ArrayList<Byte>();
		FileInputStream fileInputStream = null;
		try {
			fileInputStream = new FileInputStream(file);
			int len = (int)file.length();
			byte[] bbuf = new byte[len];
			fileInputStream.read(bbuf, 0, len);
			for (int i = 0; i < len; i++){
				arrayList.add(bbuf[i]);
			}
			fileInputStream.close();
		} catch (IOException e) {
			if (fileInputStream != null) {
				fileInputStream.close();
			}
		}
		return arrayList;
	}

	public static boolean writeFile(File file, String string) throws IOException{
		FileWriter fileWriter = null;
		boolean r = false;
		try {
			fileWriter = new FileWriter(file);
			fileWriter.write(string);
			fileWriter.close();
			r = true;
		} catch (IOException e) {
			if (fileWriter != null) {
				fileWriter.close();
			}
		}
		return r;
	}
	
	public static boolean writeFile(File file, Collection<String> collection) throws IOException{
		FileWriter fileWriter = null;
		boolean r = false;
		try {
			fileWriter = new FileWriter(file);
			for (String s : collection){
				fileWriter.write(s+"\n");
			}
			fileWriter.close();
			r = true;
		} catch (IOException e) {
			if (fileWriter != null) {
				fileWriter.close();
			}
		}
		return r;
	}
}
